package com.dandelion.api.service;

import com.dandelion.api.dao.UserAddressDao;
import com.dandelion.common.dao.auto.mapper.UserAddressMapper;
import com.dandelion.common.dao.auto.pojo.UserAddress;
import com.dandelion.common.dao.auto.pojo.UserAddressCriteria;
import com.dandelion.common.domain.UserAddressVO;
import com.rockit.core.pojo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressService {
    @Autowired
    private UserAddressDao userAddressDao;

    @Autowired
    private UserAddressMapper userAddressMapper;

    public List<UserAddressVO> getUserAddressList(long userId, Pagination pagination) {
        return userAddressDao.query(userId, pagination);
    }

    public void deleteUserAddress(long userId, long addrId) {
        userAddressDao.delete(userId, addrId);
    }

    public void updateUserAddress(long userId, long addrId, UserAddress address) {
        UserAddressCriteria criteria = new UserAddressCriteria();
        criteria.createCriteria().andIdEqualTo(addrId).andUserIdEqualTo(userId);
        userAddressMapper.updateByExampleSelective(address, criteria);
    }

    public void addNewUserAddress(long userId, UserAddress address) {
        address.setUserId(userId);
        address.setCreateTime(new Date());
        userAddressMapper.insertSelective(address);
    }
}
